package com.it.security.config;


import com.it.security.filter.JWTAuthenticationFilter;
import com.it.security.handle.FairAuthHandler;
import com.it.security.handle.JWTAccessDeniedHandler;
import com.it.security.handle.JWTAuthenticationEntryPoint;
import com.it.security.handle.SuccessfulAuthHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @program: learning
 * @description: security 认证授权配置
 * @author: Lefnmg
 * @create: 2018-11-15 16:39
 **/
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    JWTAuthenticationFilter jwtAuthenticationFilter;
    @Autowired
    FairAuthHandler fairAuthHandler;
    @Autowired
    SuccessfulAuthHandler successfulAuthHandler;
    @Autowired
    JWTAuthenticationEntryPoint jwtAuthenticationEntryPoint;
    @Autowired
    JWTAccessDeniedHandler jwtAccessDeniedHandler;

    /**
     * 编码格式密码校验
     * @return
     */
    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
            http
                //关闭csrf 攻击 ，关闭session
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                // 认证前添加jwt 过滤器
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                //所有请求都需要认证
                .authorizeRequests()
                ///auth 开头的路径无需认证
                .antMatchers("/auth/**").permitAll()
                .anyRequest()
                //自定义授权路径
                .access("@rbacService.hasPermission(request, authentication)")
                //登陆认证配置  （UsernamePasswordAuthenticationFilter 过滤器）
                .and()
                .formLogin()
                // 自定义认证登陆的路径（默认 UsernamePasswordAuthenticationFilter构造器里的/login）
                .loginProcessingUrl("/sys/login")
                .successHandler(successfulAuthHandler)
                .failureHandler(fairAuthHandler)
                //自定义人认证失败处理
//                .loginPage("/auth/require")
                .permitAll()
                .and()
                //未认证自定义处理
                .httpBasic().authenticationEntryPoint(jwtAuthenticationEntryPoint)
                .and()
                //路径未授权自定义
                .exceptionHandling().accessDeniedHandler(jwtAccessDeniedHandler);

    }
}
